package com.ruoyi.web.controller.car;


import com.ruoyi.car.business.domain.BpmnInfo;
import com.ruoyi.car.business.query.BpmnInfoQuery;
import com.ruoyi.car.business.service.IBpmnInfoService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TablePageInfo;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/*
 * 岗位控制器
*/

@Controller
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix+"deploy";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
        return new TablePageInfo<>(bpmnInfoService.query(qo));
    }

    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String filePath = bpmnInfoService.upload(file);
        return AjaxResult.success("文件上传成功",filePath);
    }

    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path, String bpmnType, String info) throws IOException {
        bpmnInfoService.deploy(path,bpmnType,info);
        return AjaxResult.success();
    }

    @RequestMapping("/readResource")
    @ResponseBody
    public void readResource(HttpServletResponse response, String deploymentId, String type) throws IOException {
        InputStream inputStream = bpmnInfoService.getFileInputStream(deploymentId,type);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }
}
